package com.yidao.template.controller;

import java.io.IOException;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.yidao.core.controller.BaseController;
import com.yidao.template.domain.Template;
import com.yidao.template.service.TemplateService;

@Controller
@RequestMapping("/templates")
public class TemplateController extends BaseController {

	@Autowired
	private TemplateService templateService;

	@ExceptionHandler(IOException.class)
	public String handleIOException(IOException ex) {
		return ClassUtils.getShortName(ex.getClass());
	}

	@RequestMapping("/")
	public ModelAndView index() {
		return new ModelAndView("/templates/main");
	}

	@RequestMapping("/{id}/preview")
	public ModelAndView preview(@RequestParam("id") Long id) {
		ModelAndView mv = new ModelAndView("/templates/preview");
		Template template = templateService.getById(id);
		mv.addObject("template", template);
		return mv;
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public ModelAndView editNew() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/templates/edit");
		Template template = new Template();
		mv.addObject("template", template);
		return mv;
	}

	@RequestMapping(value = "/new", method = RequestMethod.POST)
	public ModelAndView saveNew(
			@ModelAttribute("template") @Valid Template template,
			@RequestParam("file") MultipartFile file) throws IOException {
		template.setFileName(template.getName());
		template.setCreateUser(super.getCurrentUser());
		templateService.create(template, file.getInputStream());
		return list();
	}

	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public ModelAndView edit(@PathVariable long id) {
		Template template = templateService.getById(id);
		ModelAndView mv = new ModelAndView("/templates/edit");
		mv.addObject("template", template);
		return mv;
	}

	@RequestMapping(value = "/{id}/edit", method = RequestMethod.POST)
	public ModelAndView saveEdit(@PathVariable long id,
			@ModelAttribute("template") @Valid Template template,
			@RequestParam("file") MultipartFile file) throws IOException {

		Template oldTemplate = templateService.getById(id);
		oldTemplate.setName(template.getName());
		oldTemplate.setDescription(template.getDescription());
		oldTemplate.setFileName(file.getName());
		templateService.update(oldTemplate, file.getInputStream());
		return list();
	}

	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public ModelAndView delete(@PathVariable long id) {
		templateService.deleteById(id);
		return list();
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		List<Template> templates = templateService.listAll();
		ModelAndView mv = new ModelAndView("/templates/list");
		mv.addObject("templates", templates);
		return mv;
	}

	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}
}
